<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wallet
 * Libraray
 * @author win7
 */
class wallet {

    //put your code here
    var $CI;

    public function __construct() {
        $this->CI = &get_instance();
    }

    public function account($user_id) {
        return  $this->get_account_plus($user_id) - $this->get_account_minus($user_id);

    }

    public function get_account_minus($user_id) {
        $CI = &get_instance();
        $CI->db->select_sum("amount");
        $CI->db->select_sum("discount");
        $CI->db->where_in('status', array('pending','active','wait'));
        $sql = $CI->db->get_where('wallet',array('operation' => 'minus', 'user_id' =>$user_id));
        $row = $sql->row();
        return $row->amount+$row->discount;
    }

    public function get_account_plus($user_id) {
        $CI = &get_instance();
        $CI->db->select_sum("amount");
        
        $CI->db->where_in('status', array('active'));
        $sql = $CI->db->get_where('wallet',array('operation' => 'plus', 'user_id' =>$user_id));
        $row = $sql->row();
        return $row->amount;
    
    }

}

?>
